<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    $mes="";
    if (isset($_SESSION['mes'])) {
        $mes = $_SESSION['mes'];
        $_SESSION['mes']="";
    }
    include ("../include/menu.php");
    include ("../include/dbFunctions.php");
    $future_date = date('Y-m-d', strtotime('+7 days'));
    $todays_date=date('Y-m-d');
    //upcoming
    $event_arrayu = $event_arrayo = Array();
    //NEW
    $query3 = "SELECT * FROM `event` WHERE event_status='requested'";
    $event_array3 = executeSelectQuery($query3);
    //ongoing and upcoming
    $query4 = "SELECT DISTINCT(e.event_name), e.event_status, e.event_id FROM event e
        WHERE event_status = 'confirmed' OR event_status = 'pending'
        ORDER BY event_status DESC";
    $event_array4 = executeSelectQuery($query4);
    //now calculate ongoing and upcoming, okay?

    for ($i=0;$i<count($event_array4);$i++) {
        $query51 = "SELECT * FROM session WHERE event_id = ".$event_array4[$i]['event_id'];
        $session_array = executeSelectQuery($query51);

        for ($m=0;$m<count($session_array);$m++) {
            $event_array4[$i]['first'] = $event_array4[$i]['last'] = $session_array[0]['session_date'];
            if ($session_array[$m]['session_date']<$event_array4[$i]['first']) {
                $event_array4[$i]['first'] = $session_array[$m]['session_date'];
            }
            if ($session_array[$m]['session_date']>$event_array4[$i]['last']) {
                $event_array4[$i]['last'] = $session_array[$m]['session_date'];
            }
            //
            //
            $today = date("Y-m-d");
        }if($event_array4[$i]['first']>$today) {
            $event_array4[$i]['status']='Upcoming';
        } elseif ($event_array4[$i]['last']>$today) {
            $event_array4[$i]['status']="Ongoing";
        } else {
            $event_array4[$i]['status']="Past";
        }
        if ($event_array4[$i]['first']>$today){
             $event_arrayu[]=$event_array4[$i];
        } if ($event_array4[$i]['last']>$today){
            $event_arrayo[]=$event_array4[$i];
        }
    }
    //now count
    $no_on = 0;
    for($n=0;$n<count($event_array4);$n++) {
        if (($event_array4[$n]['status']=='Upcoming')||($event_array4[$n]['status']=='Ongoing')) {
            $no_on +=1;
        }
    }
    if (isset($_SESSION['mes'])) {
        $mes = $_SESSION['mes'];
        $_SESSION['mes']="";
    }

    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>

        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/dataTable.js"></script>
        <script type="text/javascript" src="../media/js/jquery/addInPlace.js"> </script>

        <!--  checkbox styling script -->
        <script src="../media/js/jquery/ui.core.js" type="text/javascript"></script>
        <script src="../media/js/jquery/ui.checkbox.js" type="text/javascript"></script>
        <script src="../media/js/jquery/jquery.bind.js" type="text/javascript"></script>

        <!--  styled select box script version 2 -->
        <script src="../media/js/jquery/jquery.selectbox-0.5_style_2.js" type="text/javascript"></script>
        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
        <link type="text/css" href="../media/css/dataTable.css" rel="stylesheet" />

        <!-- Manage table script -->
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $('#admintable').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
            } );
            $(document).ready(function() {
                $('#admintable1').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
            } );
        </script>

    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
            <?php
            top_outer();
    ?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
                    <?php
                    nav_right();
    ?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

                            <?php
                            navigation();
    ?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>Admin Homepage</h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <table width="100%">
                                    <td align="right">
                                            <table>
                                                <tr><td>
                                                        <input type="button" value="&nbsp;" class="tutorials" onClick="parent.location='tutorials.php'"></input>
                                                    </td>                                                   
                                                </tr>
                                            </table>

                                        </td>
                                </table>
                                <!--  start table-content  -->

                                <div id="table-content">
                                        <?php
                                        date_default_timezone_set('Asia/Singapore');
                                        $date = date('m/d/Y h:i:s a', time());
    ?>
                                    <p>Today is <?php echo $date ?></p><br/>
                                    <h3>Notification</h3>
                                    <!--  Can put table start from here within this div--> <!--lyedit-->
                                    <p>Hello Admin! Welcome back to Workspace.</p>
                                    <p>There are <?php echo count($event_array3) ?> <a href="../event/eventManagePend.php">new events</a></p><br/>
                                    <h3>Happening in next 7 days</h3>
    <?php echo "<b><font color =red>$mes </font></b>" ?>
                                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Event Name</th>
                                                <th>Start</th>
                                                <th>Start Date</th>
                                                <th>End Date</th>
                                                <th>Task before event</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <?php
                                                for ($i=0;$i<count($event_arrayu);$i++) {
                                                    $no = $i+1;
                                                    $id = $event_arrayu[$i]['event_id'];
                                                    $name =$event_arrayu[$i]['event_name'];
                                                    $event_date=$event_arrayu[$i]['first'];
                                                    $today=date("Y-m-d");
                                                    $date_difference=abs(strtotime($event_date) - strtotime($today));
                                                    $years = floor($date_difference / (365*60*60*24));
                                                    $months = floor(($date_difference - $years * 365*60*60*24) / (30*60*60*24));
                                                    $days = floor(($date_difference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

                                                    if ($days<8) {
                                                        //track
                                                        $query= "SELECT * FROM track_detail d,track_criteria t
                                                                WHERE d.track_id=t.track_id
                                                                AND d.event_id = $id
                                                                AND t.track_description ='before'";
                                                        $task_array = executeSelectQuery($query);
                                                        $taskall = count($task_array);
                                                        $done = 0;
                                                        for ($l=0;$l<$taskall;$l++) {
                                                            if ($task_array[$l]['track_detail']==1) {
                                                                $done+=1;
                                                            }
                                                        }
                                                        if ($taskall==$done) {
                                                            $track_status  = "<img src='../media/images/forms/check_icon.png' height ='20'>";
                                                        } else {
                                                            $track_status="<img src='../media/images/forms/cross_icon.png' height ='20'>";
                                                        }
                                                        echo "<tr>";

                                                        echo "<td>$no</td>";
                                                        echo "<td><a href='../event/eventView.php?id=".$id."'>$name</a></td>";
                                                        if($days==0) {
                                                            echo "<td><font color =green><b>Start today</b></font></td>";
                                                        }else {
                                                            echo "<td><font color =green><b>Start in $days days</b></font></td>";
                                                        }
                                                        echo "<td>".$event_arrayu[$i]['first']."</td>";
                                                        echo "<td>".$event_arrayu[$i]['last']."</td>";
                                                        echo "<td>".$track_status."</td>";
                                                        echo "</tr>";
                                                    }
    }?>
                                        </tbody>
                                    </table>
                                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable1">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Event Name</th>
                                                <th>End</th>
                                                <th>Start Date</th>
                                                <th>End Date</th>
                                                <th>Task after event</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <?php
                                                for ($j=0;$j<count($event_arrayo);$j++) {
                                                    $no = $j+1;
                                                    $id = $event_arrayo[$j]['event_id'];
                                                    $name =$event_arrayo[$j]['event_name'];
                                                    $event_date=$event_arrayo[$j]['last'];
                                                    $today=date("Y-m-d");
                                                    $date_difference=abs(strtotime($event_date) - strtotime($today));
                                                    $years = floor($date_difference / (365*60*60*24));
                                                    $months = floor(($date_difference - $years * 365*60*60*24) / (30*60*60*24));
                                                    $days = floor(($date_difference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
                                                    if ($days<8){
                                                        //track
                                                        $query= "SELECT * FROM track_detail d,track_criteria t
                                                                WHERE d.track_id=t.track_id
                                                                AND d.event_id = $id
                                                                AND t.track_description ='after'";
                                                        $task_array = executeSelectQuery($query);
                                                        $taskall = count($task_array);
                                                        $done = 0;
                                                        for ($l=0;$l<$taskall;$l++) {
                                                            if ($task_array[$l]['track_detail']==1) {
                                                                $done+=1;
                                                            }
                                                        }
                                                        if ($taskall==$done) {
                                                            $track_status  = "<img src='../media/images/forms/check_icon.png' height ='20'>";
                                                        } else {
                                                            $track_status="<img src='../media/images/forms/cross_icon.png' height ='20'>";
                                                        }
                                                        echo "<tr>";
                                                        echo "<td>$no</td>";
                                                        echo "<td><a href='../event/eventView.php?id=".$id."'>$name</a></td>";
                                                        if($days==0) {
                                                            echo "<td><font color =red><b>End today</b></font></td>";
                                                        }else {
                                                            echo "<td><font color =red><b>End in $days days</b></font></td>";
                                                        }
                                                        echo "<td>".$event_arrayo[$j]['first']."</td>";
                                                        echo "<td>".$event_arrayo[$j]['last']."</td>";
                                                        echo "<td>".$track_status."</td>";
                                                        echo "</tr>";
                                                    }
                                                }
    ?>
                                        </tbody>
                                    </table>
                                    <br/><br/><br/><br/>
                                    <a href="../track/trackView.php">Manage My Tracking</a>
                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>
                <div class="clear">&nbsp;</div>

            </div>
            <!--  end content -->
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
            <?php
            footer();
    ?>

    </body>
</html>
    <?php
}else {
    header("Location: login.html");
}

?>